<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="姓名" prop="nickName">
            <el-input v-model="queryParams.nickName" placeholder="请输入用户昵称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="代理区域" prop="proxyRegion">
            <el-input v-model="queryParams.proxyRegion" placeholder="请输入代理区域（例如：北京市）" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="日期" prop="proxyRegion">
            <el-date-picker
              clearable
              @change="handleDateChange"
              v-model="dateTime"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="代理区域" align="center" prop="proxyRegion" />
      <el-table-column label="人数" align="center" prop="numberPeople" />
      <el-table-column label="收入" align="center" prop="totalRevenue" />
      <el-table-column label="广告数量" align="center" prop="adNumber" />
      <el-table-column label="代理过期时间" align="center" prop="approvalTime" />
      <!--      <el-table-column label="操作" align="center" width="180" fixed="right">-->
      <!--        <template #default="scope">-->
      <!--          <el-tooltip content="详情" placement="top">-->
      <!--            <el-button link type="primary" icon="View" @click="handleUpdate(scope.row)" v-hasPermi="['system:user:edit']"></el-button>-->
      <!--          </el-tooltip>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!--    </el-card>-->
  </div>
</template>

<script setup name="User" lang="ts">
import { agentUserListApi, getUser, delUser, addUser, updateUser } from '@/api/business/user';
import { UserVO, UserQuery, UserForm } from '@/api/business/user/types';
import {getByKeyApi} from '@/api/business/config';
import {BusinessUserVO} from '@/api/business/businessUser/types';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;


const userList = ref<UserVO[]>([]);
const buttonLoading = ref(false);
const vipList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const dateTime = ref(null);

const queryFormRef = ref<ElFormInstance>();
const userFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: UserForm = {
  userId: undefined,
  deptId: undefined,
  userName: undefined,
  nickName: undefined,
  userType: undefined,
  email: undefined,
  phonenumber: undefined,
  openid: undefined,
  unionid: undefined,
  sex: undefined,
  avatar: undefined,
  password: undefined,
  amount: undefined,
  allAmount: undefined,
  status: undefined,
  loginIp: undefined,
  loginDate: undefined,
  remark: undefined,
  qrCode: undefined,
  superiorId: undefined,
  wxAvatar: undefined,
  ifCardReview: undefined,
  reasonsReview: undefined,
  idCardId: undefined,
  idCardNegativeId: undefined,
  sfzXm: undefined,
  sfzHm: undefined,
  idCardIdSczm: undefined,
  hyNumber: undefined,
  isProxy: undefined,
  gqTime: undefined
}
const data = reactive<PageData<UserForm, UserQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: undefined,
    userName: undefined,
    nickName: undefined,
    userType: undefined,
    email: undefined,
    phonenumber: undefined,
    openid: undefined,
    unionid: undefined,
    sex: undefined,
    avatar: undefined,
    password: undefined,
    amount: undefined,
    allAmount: undefined,
    status: undefined,
    loginIp: undefined,
    loginDate: undefined,
    qrCode: undefined,
    superiorId: undefined,
    wxAvatar: undefined,
    ifCardReview: undefined,
    reasonsReview: undefined,
    idCardId: undefined,
    idCardNegativeId: undefined,
    sfzXm: undefined,
    sfzHm: undefined,
    idCardIdSczm: undefined,
    hyNumber: undefined,
    isProxy: undefined,
    gqTime: undefined,
    params: {
    }
  },
  rules: {
    userId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    deptId: [
      { required: true, message: "部门ID不能为空", trigger: "blur" }
    ],
    userName: [
      { required: true, message: "用户账号不能为空", trigger: "blur" }
    ],
    nickName: [
      { required: true, message: "用户昵称不能为空", trigger: "blur" }
    ],
    userType: [
      { required: true, message: "用户类型不能为空", trigger: "change" }
    ],
    email: [
      { required: true, message: "用户邮箱不能为空", trigger: "blur" }
    ],
    phonenumber: [
      { required: true, message: "手机号码不能为空", trigger: "blur" }
    ],
    openid: [
      { required: true, message: "openid不能为空", trigger: "blur" }
    ],
    unionid: [
      { required: true, message: "unionid不能为空", trigger: "blur" }
    ],
    sex: [
      { required: true, message: "用户性别不能为空", trigger: "change" }
    ],
    avatar: [
      { required: true, message: "头像地址不能为空", trigger: "blur" }
    ],
    password: [
      { required: true, message: "密码不能为空", trigger: "blur" }
    ],
    amount: [
      { required: true, message: "积分不能为空", trigger: "blur" }
    ],
    allAmount: [
      { required: true, message: "总花费金额 用于用户vip等级评定不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "帐号状态不能为空", trigger: "change" }
    ],
    loginIp: [
      { required: true, message: "最后登录IP不能为空", trigger: "blur" }
    ],
    loginDate: [
      { required: true, message: "最后登录时间不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
    qrCode: [
      { required: true, message: "二维码不能为空", trigger: "blur" }
    ],
    superiorId: [
      { required: true, message: "上级不能为空", trigger: "blur" }
    ],
    wxAvatar: [
      { required: true, message: "微信头像地址不能为空", trigger: "blur" }
    ],
    ifCardReview: [
      { required: true, message: "0未审核 1待审核 2审核通过 3审核未通过不能为空", trigger: "change" }
    ],
    reasonsReview: [
      { required: true, message: "审核理由不能为空", trigger: "blur" }
    ],
    proxyRemarks: [
      { required: true, message: "代理审核理由不能为空", trigger: "blur" }
    ],
    idCardId: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    idCardNegativeId: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    sfzXm: [
      { required: true, message: "身份证姓名不能为空", trigger: "blur" }
    ],
    sfzHm: [
      { required: true, message: "身份证号码不能为空", trigger: "blur" }
    ],
    idCardIdSczm: [
      { required: true, message: "手持身份证正面不能为空", trigger: "blur" }
    ],
    hyNumber: [
      { required: true, message: "会员次数不能为空", trigger: "blur" }
    ],
    gqTime: [
      { required: true, message: "会员过期时间不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await agentUserListApi(queryParams.value);
  userList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}
const getByKey = async () => {
  const res = await getByKeyApi('vip');
  vipList.value = res.data;
};

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  userFormRef.value?.resetFields();
}
const handleDateChange= (value: string, dateString: any) => {
  console.log("value", value);
  queryParams.value.startTime = value[0]
  queryParams.value.endTime = value[1]
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value.startTime = '';
  queryParams.value.endTime = '';
  dateTime.value=[]
  handleQuery();
}


onMounted(() => {
  getList();
  getByKey();
});
</script>
